<template>
  <div class="list" ref="wrapper">
    <div class="content">
      <div class="area">
        <div class="title border-topbottom">你的位置</div>
        <div class="button-list">
          <div class="button-wrapper" @click="handleCityClick(city)">
            <div class="button">{{ this.city }}</div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="hotcity of hotCities" :key="hotcity.id" @click="handleCityClick(hotcity.name)">
            <div class="button">{{ hotcity.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(city, key) of cities" :key="key">
        <div class="title border-topbottom" :ref="key">{{ key }}</div>
        <div class="item-list" v-for="c of city" :key="c.id" @click="handleCityClick(c.name)">
          <div class="item border-bottom">{{ c.name }}</div>
        </div>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    change: String
  },
  methods: {
    handleCityClick (city) {
      this.$store.dispatch('changeCity', city)
      this.$router.push('/')
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    change () {
      this.scroll.scrollToElement(this.$refs[this.change][0])
    }
  },
  computed: {
    ...mapState(['city'])
  }
}
</script>

<style lang="stylus" scoped>
  .border-topbottom
    &:before
      border-color #ccc
    &:after
      border-color #ccc
  .border-bottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
  .list
    overflow: hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: .54rem
      background: #f2f2f2
      padding-left .2rem
      color: #666
      font-size .26rem
    .button-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .button-wrapper
        width: 33.33%
        float: left
        .button
          text-align: center
          margin .1rem
          padding: .1rem 0
          border: .02rem solid #ccc
          border-radius: .06rem
    .item-list
      .item
        line-height: .76rem
        color: #666
        padding-left .2rem
</style>
